//
//  FaceModel.m
//  Comma
//
//  Created by liuchang on 15/2/5.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FaceModel.h"
#import "ImageDB.h"
#import "GDataXMLNode.h"

@implementation FaceModel
//
//+ (NSArray *)getImageByString:(NSString *)faceStr
//{
//    NSMutableArray * resultArr = [NSMutableArray array];
//    //    faceStr = [faceStr substringToIndex:[faceStr length] - 4];
//    //    faceStr = [[faceStr componentsSeparatedByString:@"_"] lastObject];
//    //    NSArray * arr = [ImageDB searchAllDic];
//    //    UIImage * image;
//    //    for (NSDictionary * element in arr) {
//    //        if ([[element objectForKey:@"name"] isEqualToString:faceStr]) {
//    //            image = [UIImage imageWithData:[element objectForKey:@"imaData"]];
//    //            break;
//    //        }
//    //    }
//    //    if (image) {
//    //        return image;
//    //    }
//    //    return [UIImage imageWithData:[[arr firstObject] objectForKey:@"imaData"]];
//    NSString *str = [NSString stringWithFormat:@"<root>%@</root>", faceStr];
//
//
//    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:str options:0 error:nil];
//    GDataXMLElement *ele = [document rootElement];
//    NSArray *array = [ele elementsForName:@"img"];
//    for (GDataXMLElement *child in array) {
//        NSLog(@"array = %@", child.XMLString);
//        GDataXMLNode *node = [child attributeForName:@"alt"];
//        NSLog(@"value = %@", node.stringValue);
//        str = [str stringByReplacingOccurrencesOfString:child.XMLString withString:[NSString stringWithFormat:@"[:%@]", node.stringValue]];
//    }
//
//    if ([str hasPrefix:@"<root>"]) {
//        str = [str substringFromIndex:6];
//        str = [str substringToIndex:[str length] - 7];
//    }
//
//    NSArray * spArr = [str componentsSeparatedByString:@"["];
//    for (NSString * element in spArr) {
//        if ([element isEqualToString:@""]) {
//            continue;
//        }
//        if ([element hasPrefix:@":"]) {
//            NSString * str = [[[element componentsSeparatedByString:@"]"] firstObject] substringFromIndex:1];
//            NSString * other = [[element componentsSeparatedByString:@"]"] lastObject];
//            NSArray * arr = [ImageDB searchAllDic];
//            UIImage * image;
//            for (NSDictionary * nelement in arr) {
//                if ([[nelement objectForKey:@"name"] isEqualToString:str]) {
//                    image = [UIImage imageWithData:[nelement objectForKey:@"imaData"]];
//                    break;
//                }
//            }
//            if (!image) {
//                image = [UIImage imageNamed:@""];
//            }
//            [resultArr addObject:image];
//            if (![other isEqualToString:@""]) {
//
//                [resultArr addObject:other];
//            }
//        }
//        else {
//            [resultArr addObject:element];
//        }
//    }
//    return resultArr;
//}

+ (NSArray *)getImageByString:(NSString *)faceStr
{
    //
    NSArray * arr = [ImageDB searchAllDic];
    UIImage * image;
    NSMutableArray *allValues = [NSMutableArray array];
    NSRange imageRange = [faceStr rangeOfString:@"<img src="];
    
    if (imageRange.length == 0) {
        NSAttributedString * string = [[NSAttributedString alloc] initWithString:faceStr];
        [allValues addObject:string];
        return allValues;
    }
    
    
    while (imageRange.length > 0) {
        // 判断图片是否是从头开始，如果不是，则证明开头是文字，将文字进行截取，获得剩余的字符
        if (imageRange.location > 0) {
            NSString *strValue = [faceStr substringWithRange:NSMakeRange(0, imageRange.location)];
            NSAttributedString * string = [[NSAttributedString alloc] initWithString:strValue];
            [allValues addObject:string];
            
            faceStr = [faceStr substringFromIndex:imageRange.location];
        }
        // 最终图片肯定是从0开始，则进行截取图片，并取内容
        NSRange endImage = [faceStr rangeOfString:@"/>"];
        
        NSString *faceImage = [faceStr substringWithRange:NSMakeRange(0,endImage.location + endImage.length)];
        if (faceStr.length > faceImage.length) {
            faceStr = [faceStr substringFromIndex:endImage.location + endImage.length];
        } else {
            faceStr = @"";
        }
        
        NSRange findRange = [faceImage rangeOfString:@"motion_"];
        if (findRange.length > 0) {
            
            NSString *value = [faceImage substringFromIndex:findRange.location + findRange.length];
            NSRange pngRange = [value rangeOfString:@".png"];
            value = [value substringToIndex:pngRange.location];
            
            value = [NSString stringWithFormat:@"[:%@]", value];
            for (NSDictionary * nelement in arr) {
                if ([[nelement objectForKey:@"code"] isEqualToString:value]) {
                    image = [UIImage imageWithData:[nelement objectForKey:@"imaData"]];
                    break;
                }
            }
            if (!image) {
                image = [UIImage imageNamed:@""];
            }
            
            [allValues addObject:image];
        }
        imageRange = [faceStr rangeOfString:@"<img src="];
    }
    NSLog(@"values = %@", allValues);
    return allValues;
}




@end


